package com.lxt.cloud.entity;
import com.lxt.common.vo.RoleModel;
import com.lxt.common.vo.UserModel;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.core.user.OAuth2UserAuthority;
import org.springframework.util.CollectionUtils;

import java.util.*;


/**
 * 自定义UserDetails类 携带User实例
 */
public class MyUserDetails implements UserDetails {

    private  Long userId;
    private UserModel currentSysUser;
    private  String roleCodes;

    /**
     * 获取当前用户所具有的角色
     *
     * @return
     */
    public MyUserDetails(UserModel user) {
        if (user != null) {
            this.currentSysUser = user;
            this.userId = user.getId();
            if (!CollectionUtils.isEmpty(user.getRoleModelList())){
                StringJoiner roleCodes = new StringJoiner(",", "[", "]");
                user.getRoleModelList().forEach(e -> roleCodes.add(e.getRoleCode()));
                this.roleCodes = roleCodes.toString();
            }
        }
    }
    /**
     * 获取当前用户所能访问的资源
     *
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        if (this.currentSysUser != null && !CollectionUtils.isEmpty(this.currentSysUser.getRoleModelList())) {
            for (RoleModel role : this.currentSysUser.getRoleModelList()) {
                Map<String, Object> attributes = new HashMap<>();
                attributes.put("userId", this.getUserId());
                OAuth2UserAuthority authority = new OAuth2UserAuthority(role.getRoleCode(), attributes);
//                SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role.getRoleCode());
                authorities.add(authority);
            }
        }
        return authorities;
    }


        @Override
        public String getPassword() {
            if(currentSysUser==null)
                return null;
            return currentSysUser.getPassword();
        }

        @Override
        public String getUsername() {
            if(currentSysUser==null)
                return null;
            return currentSysUser.getLoginName();
        }

        @Override
        public boolean isAccountNonExpired() {
            return true;
        }

        @Override
        public boolean isAccountNonLocked() {
            return true;
        }

        @Override
        public boolean isCredentialsNonExpired() {
            return true;
        }

        @Override
        public boolean isEnabled() {
            return true;
        }




        public String getRoleCodes() {
            return roleCodes;
        }

        public void setRoleCodes(String roleCodes) {
            this.roleCodes = roleCodes;
        }

        public Long getUserId() {
            return userId;
        }


        public UserModel getCurrentSysUser() {
            return currentSysUser;
        }

        public void setCurrentSysUser(UserModel currentSysUser) {
            this.currentSysUser = currentSysUser;
        }

        @Override
        public String toString(){
            return this.getUsername()==null?"":this.getUsername();
        }


//    public MyUserDetails(String username, String password, Collection<? extends GrantedAuthority> authorities) {
//        super(username, password, authorities);
//    }
//
//    public MyUserDetails(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
//        super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
//    }
}